/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div>
      <div class="fixed-bar">
        <div class="item-title">
          <router-link
            class="back"
            :to="{name:'system_article_list',params:{currentPage:$route.params.currentPage}}"
            title="返回列表"
          >
            <i class="fa fa-arrow-circle-o-left"></i>
          </router-link>
          <div class="subject">
            <h3>会员协议管理</h3>
            <h5>网站会员协议设置管理</h5>
          </div>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="标题" required>
          <el-input v-model="form.doc_title" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.doc_title && !form.doc_title">{{errResult.doc_title}}</p>
        </el-form-item>
        <el-form-item label="标识码" required>
          <el-input v-model="form.doc_code" size="mini"></el-input>
          <p class="err-msg" v-if="errResult.doc_code && !form.doc_code">{{errResult.doc_code}}</p>
        </el-form-item>
        <el-form-item label="帮助内容" required>
          <Ueditor :value="ueditor.value" :config="ueditor.config" ref="ue" v-if="ueditor.isShow"></Ueditor>
          <p
            class="err-msg"
            v-if="errResult.doc_content && $refs.ue.getUEContent() == ''"
          >{{errResult.doc_content}}</p>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="isLoading">确认提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  getSystemArticle,
  updateSystemArticle,
  addSystemArticle
} from "@/utils/api";
import Ueditor from "@/components/ueditor/index";
export default {
  data() {
    return {
      form: {},
      ueditor: {
        isShow: false,
        value: "",
        config: {
          serverUrl: this.apiHead +"/mall/ueditor/config?path=system_article",
          zIndex: 999,
          initialFrameWidth: "80%", //初化宽度
          initialFrameHeight: 300, //初化高度
          focus: false, //初始化时，是否让编辑器获得焦点true或false
          maximumWords: 99999,
          removeFormatAttributes:
            "class,style,lang,width,height,align,hspace,valign", //允许的最大字符数 'fullscreen',
          pasteplain: false, //是否默认为纯文本粘贴。false为不使用纯文本粘贴，true为使用纯文本粘贴
          autoHeightEnabled: true
        }
      },
      isLoading: false,
      errResult: {}
    };
  },
  created() {
    if (this.$route.params.id) {
      this.getSystemArticle();
    } else {
      this.ueditor.isShow = true;
    }
  },
  components: {
    Ueditor
  },
  methods: {
    getSystemArticle() {
      var that = this;
      getSystemArticle(this.$route.params.id).then(function(res) {
        res.target = String(res.target);
        res.is_show = String(res.is_show);
        that.ueditor.value = res.doc_content;
        that.form = res;
        that.ueditor.isShow = true;
      });
    },
    onSubmit() {
      this.isLoading = true;
      this.form.doc_content = this.$refs.ue.getUEContent();
      var that = this;
      if (this.form.doc_id) {
        //编辑
        updateSystemArticle(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "保存成功",
              onClose: function() {
                that.$router.push({ name: "system_article_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      } else {
        //添加
        addSystemArticle(that.form).then(function(res) {
          if (res.status == 1) {
            that.$message.success({
              message: "添加成功",
              onClose: function() {
                that.$router.push({ name: "system_article_list" });
              }
            });
          } else if (res.status == -1) {
            that.$message.error("参数错误");
            that.errResult = res.result;
          } else {
            that.$message.error(res.msg);
          }
          that.isLoading = false;
        });
      }
    }
  }
};
</script>

<style scoped>
>>> .el-input__inner {
  width: 300px;
}
</style>
